//
//  UIView+EmptyData.m
//  DTSTON
//
//  Created by 喻贵华 on 16/7/13.
//  Copyright © 2016年 Demo. All rights reserved.
//

#import "UITableView +EmptyData.h"
#import "UIColor+HEX.h"
@implementation UITableView (EmptyData)
- (void) tableViewDisplayWitMsg:(NSString *) message ifNecessaryForRowCount:(NSUInteger) rowCount
{
    if (rowCount == 0) {
        // Display a message when the table is empty
        // 没有数据的时候，UILabel的显示样式
        UIImageView *imageview = [[UIImageView alloc]init];
        [imageview setImage:[UIImage imageNamed:@"no_device_default"]];
        
        imageview.contentMode = UIViewContentModeCenter;
       
        
        self.backgroundView = imageview;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
     
        [self addFooterButton];
        
       
    } else {
        UIButton *btn=[self.superview viewWithTag:100];
        @synchronized (self) {
            if (btn) {
                self.backgroundView = nil;
               
                self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
                UIButton *btn=[self.superview viewWithTag:100];
                
                [btn removeFromSuperview];
                
            }
        }
      
        

    }
}
-(void)addFooterButton
{
    UIButton *btn=[self.superview viewWithTag:100];

    if (btn) {
        btn.frame = CGRectMake(SCREEN_WIDTH/2-80, self.frame.size.height-55, 160, 44);
        return;
    }
    //1.初始化Button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag=100;
    //2.设置文字和文字颜色
    [button setTitle:@"添加设备" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithHexString:@"#45AEF5"] forState:UIControlStateNormal];
    
    //3.设置圆角幅度
//    button.layer.cornerRadius = 10.0;
//    button.layer.borderWidth = 1.0;
//    
//    //4.设置frame
  button.frame = CGRectMake(SCREEN_WIDTH/2-80, self.frame.size.height-55, 160, 44);
    
    //5.设置背景色
   
    [button setBackgroundImage:[UIImage imageNamed:@"btn_experience_bg"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"btn_experience_bg_sel"] forState:UIControlStateHighlighted];
    //6.设置触发事件
    //省略
    [button addTarget:self action:@selector(AddDevice:) forControlEvents:UIControlEventTouchUpInside];
    
    //7.添加到tableView tableFooterView中
    [self.superview  addSubview: button];
    
}
-(void)AddDevice:(UIButton*)sender
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"AddDevice" object:nil];
}
@end
